package com.atguigu.gulimall.member.controller;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.SocialUser;
import com.atguigu.gulimall.member.dto.MemberRegistDto;
import com.atguigu.gulimall.member.dto.UserLoginDto;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.exception.LoginException;
import com.atguigu.gulimall.member.exception.UserExistException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author gaozz
 * @email 1183823016@qq.com
 * @date 2022-11-03 23:10:40
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity member = new MemberEntity();
        member.setNickname("张三");
        R memberCoupons = couponFeignService.memberCoupons();
        return R.ok().put("member", member).put("coupons", memberCoupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 用户注册
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistDto dto) {
        try {
            memberService.regist(dto);
        } catch (UserExistException e) {
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), e.getMessage());
        }
        return R.ok();
    }

    /**
     * 用户手机号登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginDto dto) {
        MemberEntity entity;
        try {
            entity = memberService.login(dto);
        } catch (LoginException e) {
            return R.error(BizCodeEnume.USER_LOGIN_EXCEPTION.getCode(), e.getMessage());
        }
        return R.ok().put("data", entity);
    }

    /**
     * 微博社交登录
     * @param socialUser
     * @return
     */
    @PostMapping("/oauth/login")
    public R weiboLogin(@RequestBody SocialUser socialUser) {
        MemberEntity member = memberService.oauthLogin(socialUser);
        return R.ok().put("data", member);
    }
}
